package com.muhuahealth.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.muhuahealth.dao.RoadDao;
import com.muhuahealth.entity.RoadEntity;
import com.muhuahealth.service.RoadService;
import com.muhuahealth.utils.PageQueryUtil;
import com.muhuahealth.utils.PageResult;
import com.muhuahealth.utils.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author GSK
 * @version 1.0
 * @date 2020/12/27 16:09
 */
@Service
public class RoadServiceImpl extends ServiceImpl<RoadDao, RoadEntity> implements RoadService {

    @Autowired
    RoadDao roadDao;

    @Override
    public PageResult getRoadListPage(PageQueryUtil pageQueryUtil) {

        //展示分页内容
        List<RoadEntity> roadEntityList = roadDao.findRoadListPage(pageQueryUtil);
        //计算总页数
        int totalPage =roadDao.getTotalListPage(pageQueryUtil);
        //封装分页
        PageResult pageResult = new PageResult(roadEntityList,totalPage,pageQueryUtil.getLimit(),pageQueryUtil.getPage());
        return pageResult;
    }

    @Override
    public ServerResponse saveRoadInfo(RoadEntity roadEntity) {

        int insertInfo = roadDao.saveRoadInfo(roadEntity);

        //判断待插入的数据是否大于0,即是否有待插入数据
        if(insertInfo > 0){
            return ServerResponse.createSuccess("插入成功");
        }
        return ServerResponse.createError("插入失败");
    }

    @Override
    public ServerResponse update(RoadEntity roadEntity) {

        RoadEntity road = roadDao.selectByPrimaryKey(roadEntity.getId());

        //判断是否存在待更新的数据
        if(road == null){
            return ServerResponse.createError("data not exist!");
        }

        //判断待更新的数据是否大于0
        int updateInfo = roadDao.updateRoadInfoById(roadEntity);
        if(updateInfo > 0){
            return ServerResponse.createSuccess("更新成功!");
        }
        return ServerResponse.createError("更新失败!");
    }

    @Override
    public boolean deleteById(Integer id) {

        return roadDao.deleteById(id) > 0;
    }

    @Override
    public ServerResponse searchRoadInfo(RoadEntity roadEntity) {

        return roadDao.searchRoadInfo(roadEntity);
    }

}
